﻿#region File Description
//-----------------------------------------------------------------------------
// LisaScreen.cs
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Threading;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using kinect_music_shooter.Screens;
#endregion

namespace kinect_music_shooter
{
    class LisaMenuScreen : MenuScreen
    {

        #region Fields

        kinect_music_shooter.Audio.MusicGraph musicGraph;
        MenuEntry songs;
        int counter = 0;
        string filename = "";

        #endregion

        #region Initialization

        /// <summary>
        /// Constructor.
        /// </summary>
        public LisaMenuScreen(string file, int count)
            : base("Song-Selection")
        {
            this.filename = file;
            this.counter = count;
            musicGraph = new Audio.MusicGraph();
            string[] drives = System.Environment.GetLogicalDrives();
            DirectoryInfo[] directories;
            FileInfo[] files;
            string[] allStuff;
            string text;

            if (filename == "")
            {
                songs = new MenuEntry("back to Menu");
                songs.Selected += mainMenuSelected;
                MenuEntries.Add(songs);

                foreach (string dr in drives)
                {
                    System.IO.DriveInfo di = new System.IO.DriveInfo(dr);
                    text = di.Name.Replace("\\","/");
                    songs = new MenuEntry(text);
                    songs.Selected += folderSelected;
                    MenuEntries.Add(songs);
                }
            }
            else
            {
                if (counter > 0)
                {
                    MenuEntry upMenuEntry = new MenuEntry("<-- UP -->");
                    upMenuEntry.Selected += upSelected;
                    MenuEntries.Add(upMenuEntry);
                }
                else
                {
                    MenuEntry exitMenuEntry = new MenuEntry("back");
                    exitMenuEntry.Selected += OnCancel;
                    MenuEntries.Add(exitMenuEntry);
                }

                directories = ListDirectories(new DirectoryInfo(filename));
                files = ListFiles(new DirectoryInfo(filename));

                int musicfiles = 0;

                foreach (FileInfo fi in files)
                {
                    if (fi.Name.EndsWith(".mp3") || fi.Name.EndsWith(".wma") || fi.Name.EndsWith(".wav"))
                    {
                        musicfiles++;
                    }
                }

                allStuff = new string[directories.Length+musicfiles];
                
                for (int i = 0; i < directories.Length; i++)
                {
                    allStuff[i] = directories[i].Name + "/";
                }

                int j = 0; // zum richtigen Ablegen der Files
                for (int i = 0; i < files.Length; i++)
                {
                    if (files[i].Name.EndsWith(".mp3") || files[i].Name.EndsWith(".wma") || files[i].Name.EndsWith(".wav"))
                    {
                        allStuff[directories.Length + j] = files[i].Name;  
                        j++;
                    } 
                }

                for (int i = 0; i < 5; i++)
                {
                    if ((counter + i) < allStuff.Length)
                    {
                        songs = new MenuEntry(allStuff[counter + i]);
                        if (allStuff[counter + i].Contains("/"))
                        {
                            songs.Selected += folderSelected;
                        }
                        else
                        {
                            songs.Selected += fileSelected;
                        }
                        MenuEntries.Add(songs);
                    }
                }

                if ((counter < (int)(allStuff.Length-(allStuff.Length%5)))&&(allStuff.Length > 5))
                {
                    MenuEntry downMenuEntry = new MenuEntry("<-- DOWN -->");
                    downMenuEntry.Selected += downSelected;
                    MenuEntries.Add(downMenuEntry);
                }

            }
            
        }

        #endregion

        #region HandleInput

        void mainMenuSelected(object sender, PlayerIndexEventArgs e)
        {
            LoadingScreen.Load(ScreenManager, false, e.PlayerIndex, new BackgroundScreen(), new MainMenuScreen());
        }

        new void OnCancel(object sender, PlayerIndexEventArgs e)
        {
            MenuEntry menu = (MenuEntry)sender;
            string[] ordner = filename.Split('/');
            if (ordner.Length > 2)
            {
                filename = ordner[0] + "/";
                for (int i = 1; i < ordner.Length - 2; i++)
                {
                    filename += ordner[i] + "/";
                }
            }
            else
            {
                filename = "";
            }
            LoadingScreen.Load(ScreenManager, false, e.PlayerIndex, new BackgroundScreen(), new LisaMenuScreen(filename, 0));
        }

        void folderSelected(object sender, PlayerIndexEventArgs e)
        {
            MenuEntry menu = (MenuEntry)sender;
            filename += menu.Text;
            LoadingScreen.Load(ScreenManager, false, e.PlayerIndex, new BackgroundScreen(), new LisaMenuScreen(filename, 0));
        }

        void fileSelected(object sender, PlayerIndexEventArgs e)
        {
            MenuEntry menu = (MenuEntry)sender;
            filename += menu.Text;
            LoadingScreen.Load(ScreenManager, false, e.PlayerIndex, new BackgroundScreen(), new DifficultyScreen(filename));
            KinectMusicShooterGame.musicGraph.setSong(filename);
        }

        void upSelected(object sender, PlayerIndexEventArgs e)
        {
            counter -= 5;
            LoadingScreen.Load(ScreenManager, false, e.PlayerIndex, new BackgroundScreen(), new LisaMenuScreen(filename, counter));
        }

        void downSelected(object sender, PlayerIndexEventArgs e)
        {
            counter += 5;
            LoadingScreen.Load(ScreenManager, false, e.PlayerIndex, new BackgroundScreen(), new LisaMenuScreen(filename, counter));
        }

        #endregion

        #region Methoden

        private FileInfo[] ListFiles(DirectoryInfo directory)
        {
            FileInfo[] files = directory.GetFiles("*", SearchOption.TopDirectoryOnly);
            return files;
        }

        private DirectoryInfo[] ListDirectories(DirectoryInfo directory)
        {
            DirectoryInfo[] directories = directory.GetDirectories("*", SearchOption.TopDirectoryOnly);
            return directories;
        }

        #endregion
    }
}
